home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8614 / 8614.xpi / modules / application / ReportsManager.jsm < prev    next >
Text File  |  2010-02-10  |  3KB  |  114 lines

  1. // DO NOT import this into the global namespace, but instead
  2. // import it into your own namespace wrapper
  3.  
  4. var EXPORTED_SYMBOLS = ["REPORTS_MANAGER"];
  5.  
  6. Components.utils.import("resource://glydo/utils/prototype_xul_1_6_0_3_modified.jsm");
  7. Components.utils.import("resource://glydo/utils/Utils.jsm");
  8. Components.utils.import("resource://glydo/utils/Prefs.jsm");
  9. Components.utils.import("resource://glydo/storage/LoggingDB.jsm");
  10.  
  11. var Report = Prototype.Class.create({
  12.     initialize: function(reportData,onSendSuccess) {
  13.         this.reportData = reportData;
  14.         this.onSendSuccessFunc = onSendSuccess;
  15.     },
  16.  
  17.     send: function() {
  18.         var reporting_server_url = Prefs.reporting_server_url;
  19.         if (!reporting_server_url) {
  20.             return;
  21.         }
  22.         var params = {
  23.                 clientId: this.reportData.report.ClientID,
  24.         };
  25.         if (Prefs.server_trace) {
  26.             params.useTrace = 1;
  27.         }
  28.     
  29.         if (params = Prototype.O.toQueryString(params)) {
  30.             reporting_server_url += (Prototype.S.include(reporting_server_url,'?') ? '&' : '?') + params;
  31.         }
  32.     
  33.         var reportXml = this.getReportXml();
  34.         // 
  35.       
  36.         // Create a new AJAX request for the report
  37.         this.ajax_request = new Prototype.Ajax.Request(
  38.                 reporting_server_url, {
  39.                     contentType: "text/xml",
  40.                     method: 'post',
  41.                     postBody: reportXml,
  42.                     onSuccess: Prototype.F.bind(this.onSendSuccess,this),
  43.                     onFailure: Prototype.F.bind(this.onSendFailure,this)
  44.         });
  45.         
  46.     },
  47.     
  48.     getReportXml: function() {
  49.         var reportDoc = Utils.toXml("Glydo.ClientPeriodicalReport",this.reportData.report);
  50.         var serializer = Components.classes["@mozilla.org/xmlextras/xmlserializer;1"].createInstance(Components.interfaces.nsIDOMSerializer);
  51.         var res = serializer.serializeToString(reportDoc);
  52.         return res;
  53.     },
  54.     
  55.     onSendSuccess: function(response) {
  56.         if (!response || (response.status === 0)) {
  57.             this.onSendFailure(response);
  58.             return;
  59.         }
  60.         if ((response.status !== 200)) {
  61.             
  62.             
  63.             return;
  64.         }
  65.         this.onSendSuccessFunc(this.reportData);
  66.     },
  67.     
  68.     onSendFailure: function(response) {
  69.         var error = response ? response.responseText : "Communication problem";
  70.         if (!error) {
  71.             error = "Communication problem";
  72.         }
  73.         
  74.     }
  75. });
  76.  
  77. var REPORTS_MANAGER = ({
  78.     init: function() {
  79.         this.sendReportIfNecessary();
  80.         this.reportingIntervalId = Utils.setInterval(REPORTS_MANAGER.sendReportIfNecessary,180000);
  81.     },
  82.  
  83.     destroy: function() {
  84.         if (this.reportingIntervalId) {
  85.             Utils.clearInterval(this.reportingIntervalId);
  86.             delete this.reportingIntervalId;
  87.         }
  88.     },
  89.     
  90.     sendReportIfTimeElapsed: function(report_send_interval_ms) {
  91.         var reportData = LOGGING_DB.prepareReportIfNecessary(report_send_interval_ms);
  92.         if (reportData !== null) {
  93.             reportData.report.ClientInfo = {};
  94.             var report = new Report(reportData,REPORTS_MANAGER.onReportSentSuccess);
  95.             report.send();
  96.         }
  97.     },
  98.  
  99.     sendReportIfNecessary: function() {
  100.         this.sendReportIfTimeElapsed(Prefs.report_send_interval_mins*60000);
  101.     },
  102.  
  103.     sendReportForce: function() {
  104.         this.sendReportIfTimeElapsed();
  105.     },
  106.  
  107.     onReportSentSuccess: function(reportData) {
  108.         
  109.         Glydo.LOGGING_DB.markReportAsSent(reportData);
  110.     },
  111.  
  112. });
  113.  
  114.